﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace ATM
{
    public partial class themkhachhang : Form
    {
        private SqlConnection conn;
        private DataTable dt = new DataTable("TB_USER");
        private SqlDataAdapter da = new SqlDataAdapter();
        private Boolean kt;
        private void connect()
        {

            String cn = @"Data Source=(local);Initial Catalog=ATM_PM1;Integrated Security=True";
            try
            {
                conn = new SqlConnection(cn);
                conn.Open();

            }
            catch (SqlException ex)
            {
                Console.WriteLine(ex.Message);
                MessageBox.Show("khong thanh cong");
            }
        }

        private void disconnect()
        {
            conn.Close();
            conn.Dispose();
            conn = null;
        }
        private Boolean kiemtra()
        {
            kt = true;

            if (txtten.Text == "")
            {
                MessageBox.Show("Tên khách không được để trống !", "thông báo", MessageBoxButtons.OK);
                kt = false;
                txtten.Focus();
            }
            if (txtsothe.Text == "")
            {
                MessageBox.Show("Số thẻ không được để trống!", "thông báo", MessageBoxButtons.OK);
                kt = false;
                txtsothe.Focus();
            }
            if (txtsodt.Text == "")
            {
                MessageBox.Show("Số ĐT không được để trống!", "thông báo", MessageBoxButtons.OK);
                kt = false;
                txtsodt.Focus();
            }
            if (txtdiachi.Text == "")
            {
                MessageBox.Show("Địa chỉ không được để trống!", "thông báo", MessageBoxButtons.OK);
                kt = false;
                txtsothe.Focus();
            }
            
            return kt;
        }
        public themkhachhang()
        {
            InitializeComponent();
        }

         private void themkhachhang_Load(object sender, EventArgs e)
        {

            connect();
            getdata();
            rddnam.Checked = true;
        }
         private void getdata()
         {
             SqlCommand command = new SqlCommand();
             command.Connection = conn;
             command.CommandType = CommandType.Text;
             command.CommandText = @"select * from TB_USER  ";
             da.SelectCommand = command;
             da.Fill(dt);
             
         }
         private void btnluulai_Click(object sender, EventArgs e)
         {
             kiemtra();

             if (kt == true)
             
             {
                 DataRow row = dt.NewRow();
                 row["User_ID_Card_No"] = txtsothe.Text;
                 row["User_FullName"] = txtten.Text;
                 row["User_DoB"] = dtpngaylap.Value;
                 if (rddnam.Checked)
                 {
                     row["User_Gender"] = 1;
                 }
                 else row["User_Gender"] = 0;
                 row["User_Phone"] = txtsodt.Text;
                 row["User_Address"] = txtdiachi.Text;
                 dt.Rows.Add(row);
                 SqlCommand commandInsert = new SqlCommand();
                 commandInsert.Connection = conn;
                 commandInsert.CommandType = CommandType.Text;
                 commandInsert.CommandText = @"insert into TB_USER(User_ID_Card_No,User_FullName,User_DoB,User_Gender,User_Phone,User_Address) values(@User_ID_Card_No,@User_FullName,@User_DoB,@User_Gender,@User_Phone,@User_Address)";
                 commandInsert.Parameters.Add("@User_ID_Card_No", SqlDbType.NVarChar, 30, "User_ID_Card_No");
                 commandInsert.Parameters.Add("@User_FullName", SqlDbType.NVarChar, 50, "User_FullName");
                 commandInsert.Parameters.Add("@User_DoB", SqlDbType.DateTime, 30, "User_DoB");
                 commandInsert.Parameters.Add("@User_Gender", SqlDbType.Int, 20, "User_Gender");
                 commandInsert.Parameters.Add("@User_Phone", SqlDbType.Int, 20, "User_Phone");
                 commandInsert.Parameters.Add("@User_Address", SqlDbType.NVarChar, 300, "User_Address");
                 da.InsertCommand = commandInsert;
                 da.Update(dt);
                 qlkhachhang qlkh = new qlkhachhang();
                 qlkh.Show();
                 this.Close();
             }
         }
            
         

         private void btnthoat_Click(object sender, EventArgs e)
         {
             Close();
             Dispose();
         }

         private void txtsodt_KeyPress(object sender, KeyPressEventArgs e)
         {
             int isnum = 0;
             e.Handled = !int.TryParse(e.KeyChar.ToString(),out isnum);
         }
    }
}
